let stompClient = null;  /* 连接服务器 */
let socket = new SockJS('/chat');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {

    /**
     * 接收服务器消息
     */

    stompClient.subscribe('/user/queue/chat', function(chat) {
        showGreeting(JSON.parse(chat.body));
    });
});

function sendMsg() {
    let sendData = {
        'content' : $("#content").val(),
        'to' : $("#to").val()
    };
    let dataJsonStr = JSON.stringify(sendData);
    stompClient.send("/app/chat", {}, dataJsonStr);
}

function showGreeting(message) {
    let msgHtml = "<span>" + message.from + ":" + message.content + "</span><br/>";
    $("#msglist").append(msgHtml);

    let div = document.getElementById('msglist');
    div.scrollTop = div.scrollHeight;
}

$(function() {
    $("#disconnect").click(function() {
        disconnect();
    });
    $("#send").click(function() {
        sendMsg();
    });
});